#coding:utf-8

# --------------------------------------------------------------------------------
# 审查机构的管理
# --------------------------------------------------------------------------------
module ManagerService
  def self.registered(app)

    app.get "/?" do
      ManagerRepo.all.map(&:perfect).to_json
    end
    
    app.get "/:id" do
      param :id, Integer, required: true
      elem =  ManagerRepo[params[:id]]
      elem.nil? ? raise("not found manager: #{params[:id]}") : elem.perfect.to_json
    end

    app.post "/:id/toggle" do
      param :id, Integer, required: true
      elem = ManagerRepo[params[:id]]
      if elem
        elem.toggle; elem.save.perfect.to_json
      end
    end
    
    app.post "/?" do
      param :name, String, required: true
      ManagerRepo.create(params.symbolize_keys).perfect.to_json
    end

    app.put "/:id/?" do
      param :id,   Integer, required: true
      param :name, String,  required: true
      elem = ManagerRepo[params[:id]]
      elem.update(name: params[:name]).save.perfect.to_json if elem
    end
  end
end
